<?php 
/**
 * 猜牌游戏
 */
class GuessCardGame extends APIBase {

	public $tag = "GuessCardGame";

	public function before() {
		$this->initCacheRedis();
		return true;
	}

	public function logic() {
		
		if (!isset($this->param["base_money"]))
		{
			$this->returnError(300,"base_money must needed.\n");
		}
		else 
		{
			$base_money = (int)$this->param["base_money"];
		}
		
		$money = $this->data_redis->hget("hu:{$this->uid}","money");
		if ($money < $base_money)
		{
			$this->returnError(301,"对不起您的金币不足啦");
		}
		$add = -$base_money;	
		$card_name_array = array("幸运奖","一倍奖励","两倍奖励","四倍奖励","十倍奖励");
		$card_type_array = array(0,1,2,4,10);
		
		$rate_array= array(0.1, 1, 2, 4, 10);
		$prop_array= array(0.4, 0.05, 0.4, 0.04, 0.002);
	
		$prop_num_array = array();
		for ($i=0; $i<count($prop_array); $i++)
		{
			$prop_num_array[$i] = $prop_array[$i] * 1000;
		}				
		$rn = rand(1,1000);		
		$temp = 0;
		$n = 0;
		for ($i=0; $i<count($prop_num_array); $i++)
		{
			$temp += $prop_num_array[$i];
			if ($rn <= $temp)
			{
				$n = $i;
				break;
			}		
		}
		
		for ($i =0; $i<count($rate_array); $i++)
		{
			$reward_money_array[$i] = $base_money * $rate_array[$i];
		}	
		$add += $reward_money_array[$n];
		
		$cur_money = $this->data_redis->hincrBy("hu:{$this->uid}","money", $add);
		$this->returnData(array("card_name_array" => $card_name_array ,"card_type_array"=> $card_type_array, "hit_num" => $n, "add_money" => $reward_money_array[$n], "cur_money" => $cur_money));
				
	}

	public function after() {
		$this->deinitCacheRedis();
		$this->deinitDataRedis();
	}
}

?>
